//679. 24 点游戏

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var judgePoint24 = function(nums) {

    const OP = ['+','-','*','/']
    const TARGET = 24
    const EPSILON = 1e-6

   
    //做运算的
    let calc = (a,b,op)=>{
        let ans = a
        switch(op){
            case '+':
                ans+=b
                break
            case '-':
                ans-=b
                break
            case '*':
                ans*=b
                break
            case '/':
                ans/=b
                break
        }
        return ans
    }

    /** 
     * @param {number[]} nums 
     * @return {boolean}
     */
    let search =(nums)=>{
        /**
         * !!!!!!!括号竟然是用数组传递值来表示的 
         */
        if(nums.length===0) return true
        if(nums.length===1) return Math.abs(nums[0]-TARGET)<=EPSILON
        for(let i=0;i<nums.length;i++){
            for(let j=0;j<nums.length;j++){
                if(i===j)continue
                for(let k=0;k<4;k++){
                    let op = OP[k]
                    let result = calc(nums[i],nums[j],op)
                    let newNums = []
                    for(let p=0;p<nums.length;p++){
                        if(p!==i&&p!==j){
                            newNums.push(nums[p])
                        }
                    }
                    newNums.push(result)
                    let ans = search(newNums)
                    if(ans) return true
                }
            }


        }
        return false
    }

    return search(nums)

};